package com.lvmama.pangolin.controller;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.ResultMsg;
import com.lvmama.pangolin.entity.lmm.LMMCareProduct;
import com.lvmama.pangolin.lmm.service.LMMCareProductService;
import com.lvmama.pangolin.util.SpringContextUtil;


@RestController  
@RequestMapping("syn") 
public class CareProductController
{
	@Autowired
	LMMCareProductService careProductService;
	
	private static Logger log = Logger.getLogger(CareProductController.class);
	
	@RequestMapping(value = "pushNewProd", method = RequestMethod.POST)
	public Object  queryNewProduct(@RequestBody LMMCareProduct record)
	{
		log.info("获取产品信息开始！");
		ResultMsg msg = new ResultMsg();
		msg.setSuccess("fail");
		msg.setMessage("push product data error!");
		msg.setCode("1");
		try
		{
			if(careProductService == null )
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			if(record != null)
			{
				if(StringTools.isValid(record.getProductId()))
				{
					List<LMMCareProduct> careList = new ArrayList<LMMCareProduct>();
					careList.add(record);
					careProductService.delAndInsert(careList);
					msg.setSuccess("success");
					msg.setMessage("push product data success!");
					msg.setCode("0");
				}
				
			}
			
			
		}catch (Exception e)
		{
			log.error("获取产品信息失败！",e);
		}
		log.info("获取产品信息结束！");
		return msg;
		
	}
	
	@RequestMapping(value = "pushFullProd", method = RequestMethod.POST)
	public Object  pushProduct(@RequestBody List<LMMCareProduct> record)
	{
		log.info("获取产品信息开始！");
		ResultMsg msg = new ResultMsg();
		msg.setSuccess("fail");
		msg.setMessage("push product data error!");
		msg.setCode("1");
		try
		{
			if(careProductService == null )
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			if(record != null && !record.isEmpty())
			{
				Collections.sort(record, new Comparator<LMMCareProduct>()
				{

					@Override
					public int compare(LMMCareProduct o1, LMMCareProduct o2)
					{
						return o1.getProductId().compareTo(o2.getProductId());
					}
					
				});
				String minProd = record.get(0).getProductId();
				String maxProd = record.get(record.size()-1).getProductId();
				Map<String,Object> param = new HashMap<String,Object>();
				param.put("minId", minProd);
				param.put("maxId", maxProd);
				
				careProductService.synCareProduct(record,param);
				msg.setSuccess("success");
				msg.setMessage("push product data success!");
				msg.setCode("0");
				
			}
			
			
		}catch (Exception e)
		{
			log.error("获取产品信息失败！",e);
		}
		log.info("获取产品信息结束！");
		return msg;
		
	}
	
	@RequestMapping(value = "deleteProd", method = RequestMethod.GET)
	public Object  deleteProduct(String minProduct,String maxProduct)
	{
		log.info("获取产品信息开始！");
		ResultMsg msg = new ResultMsg();
		msg.setSuccess("fail");
		msg.setMessage("push product data error!");
		msg.setCode("1");
		try
		{
			if(careProductService == null )
			{
				careProductService = (LMMCareProductService) SpringContextUtil.getBean(LMMCareProductService.class);
			}
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("minId", minProduct);
			param.put("maxId", maxProduct);
			careProductService.deleteByProdUnRange(param);
			msg.setSuccess("success");
			msg.setMessage("push product data success!");
			msg.setCode("0");
			
		}catch (Exception e)
		{
			log.error("获取产品信息失败！",e);
		}
		log.info("获取产品信息结束！");
		return msg;
		
	}
}
